package com.sx.baseproject.framework.net.base;

import android.text.TextUtils;
import android.util.Log;

import com.example.mvvm.base.cache.CacheUtils;
import com.example.mvvm.base.cache.FToken;
import com.google.gson.Gson;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;


public class PostBaseRequest extends BaseRequest {
    public PostBaseRequest() {
        super.isGet = false;
    }

    public Map<String, Object> postMap;
    public Map<String, String> getMap;

    public PostBaseRequest add(String key, Object obj) {
        if (postMap == null) {
            postMap = new HashMap<>();
        }
        if (obj != null) {
            postMap.put(key, obj);
        }
        return this;
    }

    public PostBaseRequest addGet(String key, Object obj) {
        if (getMap == null) {
            getMap = new HashMap<>();
        }
        if (obj != null) {
            getMap.put(key, obj.toString());
        }
        return this;
    }

    public RequestBody toBody() {
        Map<String, Object> keyAndValue = new HashMap<>();
        Class<? extends PostBaseRequest> aClass = this.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            if (!declaredField.isAnnotationPresent(GetParam.class)) {
                try {
                    if (!"postMap".equals(declaredField.getName())&&!"getMap".equals(declaredField.getName())) {
                        keyAndValue.put(declaredField.getName(), declaredField.get(this));
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        if (postMap != null) {
            keyAndValue.putAll(postMap);
        }
        FToken token = CacheUtils.getToken();
        if (token != null) {
            keyAndValue.put("token", token.token);
        }
        String toJson = new Gson().toJson(keyAndValue);
        Log.i("PostBaseRequest", toJson + "");
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), toJson);
        return requestBody;
    }

    public String buildMap(Map<String, String> map) {
        if (map == null || map.size() == 0) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        sb.append("?");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            sb.append(entry.getKey() + "=" + entry.getValue());
            sb.append("&");
        }
        String s = sb.toString();
        if (s.endsWith("&")) {
            s = s.substring(0, s.length() - 1);
        }
        return s;
    }

    public String buildMap(List ids, String key) {
        if (ids == null || ids.size() == 0) {
            return "";
        }
        StringBuffer sb = new StringBuffer();
        for (Object id : ids) {
            sb.append(key + "=" + id);
            sb.append("&");
        }
        String s = sb.toString();
        if (s.endsWith("&")) {
            s = s.substring(0, s.length() - 1);
        }
        if (s.startsWith(key + "=")) {
            s = s.substring((key + "=").length());
        }
        return s;
    }

    public String toGetParam() {
        Map<String, String> keyAndValue = new HashMap<>();
        if(getMap!=null){
            keyAndValue.putAll(getMap);
        }
        FToken token = CacheUtils.getToken();
        if (token != null) {
            keyAndValue.put("token", token.token);
        }else{
            keyAndValue.put("token","");
        }
        Class<? extends PostBaseRequest> aClass = this.getClass();
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            declaredField.setAccessible(true);
            boolean annotationPresent = declaredField.isAnnotationPresent(GetParam.class);
            if (annotationPresent) {
                try {
                    if (declaredField.get(this) != null) {
                        if (declaredField.get(this) instanceof List) {
                            List listData = (List) declaredField.get(this);
                            keyAndValue.put(declaredField.getName(), buildMap(listData, declaredField.getName()));
                        } else {
                            if (!TextUtils.isEmpty(String.valueOf(declaredField.get(this)))) {
                                keyAndValue.put(declaredField.getName(), String.valueOf(declaredField.get(this)));
                            }else{
                                keyAndValue.put(declaredField.getName(), "");
                            }
                        }

                    }

                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }

        return buildMap(keyAndValue);
    }
}
